****************************
* RDD Regressions
****************************

use "transactions.dta"
set more off

gen week = week(dofc(time))
gen year = year(dofc(time))
replace year = year - 2014
replace week = year*52+ week
gen month = month(dofc(time))
replace month = year*12 + month
//replace week = month

bys productid week: egen weekrev = sum(transactionvalue)
bys productid week: egen weekquan = sum(quantityinpiece)
bys productid week: egen weekbnum = nvals(buyerid)
bys productid week: egen weekdnum = nvals(buyercountry)
bys productid week: gen weekorder = _N
gen qb = weekquan/weekbnum
gen weekprice = weekrev/weekquan
gen lnweekprice = ln(weekprice)
gen tranprice = transactionvalue/quantityinpiece
gsort +productid +week +time
by productid week: gen first = _n==1
gen wprice = tranprice if first==1
bys productid week: egen wtranprice = mean(wprice)
gen lnwtranprice = ln(wtranprice)


gen lnpic = ln(picnumtotal+1)
rename sizechoice sizechoicenum
gen lnsize = ln(sizechoicenum)
gen lnitem = ln(itemnum)
gen lnword = ln(wordnum+1)
gen decoration = 1 if itemdecoration!=""
replace decoration = 0 if strpos(itemdecoration,"None")
replace decoration=0 if itemdecoration==""

preserve
tempfile tempquality
local qualityvar "materialquality fguapro freturnpolicy lnsize lnitem lnpic sellerid productrating"
keep productid `qualityvar'
duplicates drop productid, force
save `tempquality',replace
restore
drop `qualityvar'


// weekly star
foreach i of numlist 1/5{
	gen star`i' = 1 if buyerrating == `i'
	bys productid week: egen star`i'num = sum(star`i')
}
//
duplicates drop productid week, force

//fillin start
bys productid: egen entryweek = min(week)
replace entryweek = 5 if prevordernum !=0
fillin productid week
bys productid: egen entryweeknew = min(entryweek)
drop entryweek
rename entryweeknew entryweek
drop if week<entryweek
foreach i of numlist 1/5{
	replace star`i'num = 0 if star`i'num ==.
	}
xtset productid week
replace wtranprice = l.wtranprice if wtranprice==.
replace lnwtranprice = l.lnwtranprice if lnwtranprice==.
local weekvar "weekrev weekquan weekorder weekbnum weekdnum qb"
foreach k of local weekvar{
	replace `k' = 0 if `k'==.
	}
gen lnweekrev = ln(weekrev+1)
gen lnweekquan = ln(weekquan+1)
gen lnweekorder = ln(weekorder+1)
gen lnweekbuyer = ln(weekbnum+1)
gen lnweekdnum = ln(weekdnum+1)
gen lnweekqb = ln(qb+1)


//fillin end

merge m:1 productid using `tempquality'
drop if _merge==2
drop _merge

xtset productid week
foreach i of numlist 1/24{
	foreach j of numlist 1/5{
		gen lag`i'star`j' = l`i'.star`j'num
		replace lag`i'star`j' = 0 if lag`i'star`j'==.  
		}
	}
foreach j of numlist 1/5{
	gen star`j'sum = lag1star`j'
	foreach k of numlist 2/24{
		replace star`j'sum = star`j'sum + lag`k'star`j'
		}
	}
gen wtranpriceg = lnwtranprice-l.lnwtranprice
gen truerating = (star5sum*5 + star4sum*4 + star3sum*3 + star2sum*2 + star1sum)/(star1sum + star2sum + star3sum + star4sum + star5sum)
egen averating = mean(truerating)
gen orgrating = truerating/averating
gen treated= 0 if ((truerating*10 - int(truerating*10))>0 & (truerating*10 - int(truerating*10))<.5) | (truerating*10 - int(truerating*10))==0
replace  treated= 1 if (truerating*10 - int(truerating*10))>0.5 | (truerating*10 - int(truerating*10)) == .5

replace treated=. if (truerating*10 - int(truerating*10))==.
gen treated_orgrating = treated*orgrating

gen treated_rating = truerating * treated
gen prated = (truerating!=0)
gen rated_treated = treated*prated

recode truerating (1/1.999999999 = 1) (2/2.999999999 = 2) (3/3.999999999999=3) (4/5 = 4), gen(cat_rate)
replace cat_rate = 0 if cat_rate==.
qui tab cat_rate, gen(cat_ratedum)
drop cat_ratedum1 // reference group is no rating
foreach k of numlist 2/5 {
	gen dum_treat`k' = cat_ratedum`k'*treated_rating
}
local keyvar "lnweekorder truerating"
foreach k of local keyvar{
	bys productid: egen ap`k' = mean(`k')
	gen d_`k'p = ap`k' - `k'
	bys week: egen ave`k't = mean(d_`k'p)
	gen d2_`k' = ave`k't - d_`k'p
	}
egen swgroup = group(sellerid week)

local sdvar lnweekquan wtranprice cat_ratedum2 cat_ratedum3 cat_ratedum4 cat_ratedum5 treated materialquality fguapro freturnpolicy lnsize lnitem lnpic
foreach k of local sdvar {
	egen std`k'= std(`k')
}


local dep "rev quan qb bnum dnum"
//local dep "lnweekquan"
local qualityvar "materialquality fguapro freturnpolicy lnsize lnitem lnpic"
est drop _all
xtset sellerid
foreach j of local dep{
	foreach k of numlist 1/1{
	eststo rddresult`j':qui xtreg lnweekly`j' wtranprice truerating treated_rating `qualityvar' i.week, fe i(sellerid) vce(r)
	}
	}
estout rddresult*, drop(*.week) cells(b(star fmt(3)) se(par fmt(3))) stats(r2 N) starlevels(* 0.10 ** 0.05 *** 0.01)
